package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampKpiAudit;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CampKpiAuditDAO {
	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampKpiAudit transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Camp_Kpi_Audit where camp_Id="
				+ transientInstance.getCampId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Kpi_Audit set camp_seq_Id='"
						+ transientInstance.getCampSeqId() + "',camp_id='"
						+ transientInstance.getCampId()
						+ "',audit_date=timestamp '"
						+ CurrentTime.getCurrentTime() + "',Call_Total='"
						+ transientInstance.getCallTotal() + "',Inter_Total='"
						+ transientInstance.getInterTotal() + "',Accp_Total='"
						+ transientInstance.getAccpTotal() + "',Fin_Total='"
						+ transientInstance.getFinTotal() + "',Inter_Rat='"
						+ transientInstance.getInterRat() + "',Accp_Rat='"
						+ transientInstance.getAccpRat() + "',Sum_Fin_Rat='"
						+ transientInstance.getSumFinRat()
						+ "',Sum_Accp_Total='"
						+ transientInstance.getSumAccpTotal()
						+ "',Sum_Fin_Total='"
						+ transientInstance.getSumFinTotal() + "',Latn_Id="
						+ transientInstance.getLatnId() + " where camp_Id="
						+ transientInstance.getCampId();
			else
				sql = "insert into " + db + ".Camp_Kpi_Audit values('"
						+ transientInstance.getCampSeqId() + "','"
						+ transientInstance.getCampId() + "',"
						+ "',timestamp '" + CurrentTime.getCurrentTime()
						+ "','" + transientInstance.getCallTotal() + "','"
						+ transientInstance.getInterTotal() + "','"
						+ transientInstance.getAccpTotal() + "','"
						+ transientInstance.getFinTotal() + "','"
						+ transientInstance.getInterRat() + "','"
						+ transientInstance.getAccpRat() + "','"
						+ transientInstance.getSumFinRat() + "','"
						+ transientInstance.getSumAccpTotal() + "','"
						+ transientInstance.getSumFinTotal() + "','"
						+ transientInstance.getLatnId() + "')";
			rs = dao.insert(sql);
			
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampKpiAudit persistentInstance) {

		String sql = "delete from " + db + ".Camp_Kpi_Audit where camp_Id = "
				+ persistentInstance.getCampId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampKpiAudit findById(String id) {

		String sql = "select * from " + db + ".Camp_Kpi_Audit where camp_Id = "
				+ id;
		CampKpiAudit sd = new CampKpiAudit();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setCampSeqId(rs.getString(1));
				sd.setCampId(rs.getInt(2));
				sd.setAuditDate(rs.getString(3));
				sd.setCallTotal(rs.getLong(4));
				sd.setInterTotal(rs.getLong(5));
				sd.setAccpTotal(rs.getLong(5));
				sd.setFinTotal(rs.getLong(6));
				sd.setInterRat(rs.getDouble(7));
				sd.setAccpRat(rs.getDouble(8));
				sd.setSumFinRat(rs.getDouble(9));
				sd.setSumAccpTotal(rs.getLong(10));
				sd.setSumFinTotal(rs.getLong(11));
				sd.setLatnId(rs.getString(12));

			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll() {

		String sql = "select * from " + db + ".Camp_Kpi_Audit";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampKpiAudit sd = new CampKpiAudit();
				sd.setCampSeqId(rs.getString(1));
				sd.setCampId(rs.getInt(2));
				sd.setAuditDate(rs.getString(3));
				sd.setCallTotal(rs.getLong(4));
				sd.setInterTotal(rs.getLong(5));
				sd.setAccpTotal(rs.getLong(5));
				sd.setFinTotal(rs.getLong(6));
				sd.setInterRat(rs.getDouble(7));
				sd.setAccpRat(rs.getDouble(8));
				sd.setSumFinRat(rs.getDouble(9));
				sd.setSumAccpTotal(rs.getLong(10));
				sd.setSumFinTotal(rs.getLong(11));
				sd.setLatnId(rs.getString(12));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}
